# db_config.py
# 提供一个简单的 MySQL 验证函数，使用 pymysql
import pymysql
from pymysql.cursors import DictCursor
import config

def check_user_exists(user_id: str, usap_id: str) -> bool:
    """
    检查 user_auth 表中是否存在 (user_id, usap_id) 组合。
    返回 True/False。
    """
    conn = None
    try:
        conn = pymysql.connect(
            host=config.DB_HOST,
            port=config.DB_PORT,
            user=config.DB_USER,
            password=config.DB_PASS,
            database=config.DB_NAME,
            charset="utf8mb4",
            cursorclass=DictCursor,
            connect_timeout=5
        )
        with conn.cursor() as cur:
            sql = "SELECT COUNT(1) as cnt FROM user_auth WHERE user_id=%s AND usap_id=%s"
            cur.execute(sql, (user_id, usap_id))
            row = cur.fetchone()
            return bool(row and row.get("cnt", 0) > 0)
    except Exception as e:
        # 如果数据库不可用，为了安全起见返回 False（调用方会记录日志并拒绝）
        raise
    finally:
        if conn:
            conn.close()
